﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using MiniCrawler.Business;
using MiniCrawler.Logging;

namespace MiniCrawler
{
    public class Common
    {

        /// <summary>
        /// Method get Link and Title from all Tag A in page.
        /// </summary>
        /// <param name="html"></param>
        public static List<string> GetPathAndTitleTagA(string html)
        {
            try
            {
                List<string> PathTitle = new List<string>();
                String pattern = @"<A\shref=""(?<FilePath>[^""]*)"">(?<File>[^<]*)";

                Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);
                Match m = rx.Match(html);
                while (m.Success)
                {
                    string value = m.Groups["File"].Value + "$" + m.Groups["FilePath"].Value;
                    PathTitle.Add(value);

                    m = m.NextMatch();
                }

                return PathTitle;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Method get content between tow tag HTML
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string GetContentInBetweenTab(string begin, string end, string page)
        {
            try
            {
                string content = "";
                int indexFirst = page.IndexOf(begin);
                int indexEnd = page.IndexOf(end, indexFirst + begin.Length);
                if (indexFirst != -1 && indexEnd != -1 && indexEnd > indexFirst)
                {
                    content = page.Substring(indexFirst + begin.Length, indexEnd - indexFirst - begin.Length);
                }
                return content;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Get web page return code HTML
        /// </summary>
        /// <param name="strURL"></param>
        /// <returns></returns>
        public static string GetWebPage(string strURL)
        {
            try
            {

                //stores the html returned by the server
                string strResult = "";

                //build the http request
                HttpWebRequest webRequest;
                Log.WriteLog("[REQUEST TO][" + strURL + "]");
                webRequest = (HttpWebRequest)WebRequest.Create(strURL);

                IWebProxy proxy = WebRequest.GetSystemWebProxy();
                proxy.Credentials = CredentialCache.DefaultCredentials;

                webRequest.Proxy = proxy;
                webRequest.Method = "GET";
                webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
                webRequest.Timeout = 30000;

                //initiate contact with the server
                HttpWebResponse webResponse;
                webResponse = (HttpWebResponse)webRequest.GetResponse();
                
                //download the HTML
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
                {
                    //read the html
                    strResult = sr.ReadToEnd();
                    // Close and clean up the StreamReader
                    sr.Close();
                }
                Log.WriteLog("[RESPONSE LENGTH][" + strResult.Length + "]");
                return strResult;

            }
            catch (Exception ex)
            {
                LogService.Service.Error("[FAILED][URL = " + strURL + "]");
                LogService.Service.Error(ex);
                return string.Empty;
            }
        }

        /// <summary>
        /// Method GetPage
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetPage(string url)
        {
            try
            {

                string page;
                WebRequest myWebRequest;
                WebResponse myWebResponse;

                myWebRequest = WebRequest.Create(url);
                myWebResponse = myWebRequest.GetResponse();//Returns a response from an Internet resource

                Stream streamResponse = myWebResponse.GetResponseStream();//return the data stream from the internet
                //and save it in the stream

                StreamReader sreader = new StreamReader(streamResponse);//reads the data stream
                page = sreader.ReadToEnd();//reads it to the end

                streamResponse.Close();
                sreader.Close();
                myWebResponse.Close();

                return page;

            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// Method Get all link in content in put
        /// </summary>
        /// <param name="content"></param>
        /// <param name="strregex"></param>
        /// <returns></returns>
        public static List<string> GetNewLinks(string content, string strregex)
        {
            try
            {
                //Regex regex = new Regex("(?<=<a\\s*?href=(?:'|\"))[^'\"]*?(?=(?:'|\"))");
                //Regex regex = new Regex(@"<a .*?>(.*?)</a>");
                Regex regex = new Regex(strregex);
                List<string> newLinks = new List<string>();
                foreach (var match in regex.Matches(content))
                {
                    if (!newLinks.Contains(match.ToString()))
                        newLinks.Add(match.ToString());
                }

                return newLinks;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Method RemoveTagHTML
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string RemoveTagHTML(string source)
        {
            try
            {

                return Regex.Replace(source, "<.*?>", string.Empty);

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Method ReplaceTagHTML
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ReplaceTagHTML(string source)
        {
            try
            {

                return Regex.Replace(source, "<.*?>", "$");

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Method ReplaceLinkImage
        /// </summary>
        /// <param name="source"></param>
        /// <param name="strold"></param>
        /// <param name="strnew"></param>
        /// <returns></returns>
        public static string ReplaceLinkImage(string source, string strold, string strnew)
        {
            try
            {

                return Regex.Replace(source, strold, strnew);

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Method HTmlEncode
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string HtmlEndCode(string value)
        {
            try
            {

                StringBuilder sb = new StringBuilder(value);
                sb.Replace("&", "&amp;");
                sb.Replace("<", "&lt;");
                sb.Replace(">", "&gt;");
                sb.Replace("\"", "&quot;");

                return sb.ToString();

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Method xoa cap the html va noi dung bao boi the do trong page lay ve
        /// </summary>
        /// <param name="content"></param>
        /// <param name="keyfirst"></param>
        /// <param name="keyend"></param>
        /// <returns></returns>
        public static string RemoveContentAllTagHTML(string content, string keyfirst, string keyend)
        {
            try
            {

                int indexfirst = content.IndexOf(keyfirst);
                int indexend = content.IndexOf(keyend, indexfirst + keyfirst.Length);
                string result = content.Substring(indexend + keyend.Length);
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }


        public static string FillAbsoluteUrl(string hostName, string url)
        {
            if (url.StartsWith("/")) return (hostName + url).Replace("//", "/").Replace("http:/", "http://");

            else return url;
        }

    }
}
